﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Receipts.DataModel;
using CSP.EFPF.Common;
using CSP.EFPF.CodeOnly;

namespace CSP.Receipts.Business
{
    public interface ICategoryRepository : IRepository<Category, ReceiptContext>
    {
        IDictionary<string,int> GetReceiptsCountByCategory();
    }

    public class CategoryManager : BaseRepository<Category, ReceiptContext>, ICategoryRepository
    {
        public CategoryManager(IUnitOfWork<ReceiptContext> uow)
            :base(uow)
        {            
        }

        public IDictionary<string, int> GetReceiptsCountByCategory()
        {
            var list = from cat in GetAll()
                       orderby cat.Title
                       select new { Category = cat.Title, ReceiptCount = cat.Receipts.Count };
            return list.ToDictionary(item => item.Category, item => item.ReceiptCount);
        }
    }
}
